#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#define MAXM 10000
#define MAXN 10
#define INF 1000000000
using namespace std;

typedef struct{
    int loc, id;
} Cow;

int cowcmp(const void *va, const void *vb)
{
    Cow *a = (Cow *) va;
    Cow *b = (Cow *) vb;
    return a->loc > b->loc;
}

int main()
{
    freopen("lineup.txt","r", stdin);
    int num_IDS, N, i, num_in_map = 0, A[50000], IDS[50000], start, end, min;
    Cow cows[50000];
    map<int, int> breeds;
    set<int> ID_set;
    scanf("%d", &N);
    for (i=0;i<N;i++){
        scanf("%d %d", &cows[i].loc, &cows[i].id);
        ID_set.insert(cows[i].id);
        breeds[cows[i].id] = 0;
    }
    qsort(cows, N, sizeof(Cow), cowcmp);

    num_IDS = ID_set.size();

    start = 0;
    end = 0;
    min = 1 << 30;

    while (1){
        while (num_in_map != num_IDS && end < N){
            if (breeds[cows[end].id] == 0)
                num_in_map++;
            breeds[cows[end++].id]++;
        }
        if (end == N && num_in_map != num_IDS)
            break;

        while (breeds[cows[start].id] > 1)
            breeds[cows[start++].id]--;

        if (cows[end-1].loc - cows[start].loc < min)
            min = cows[end-1].loc - cows[start].loc;
        breeds[cows[start++].id]--;
        num_in_map--;
    }
    printf("%d\n", min);
    return 0;
}
